package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import pl.register.android.classes.Classes;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.database.dao.StudentLessonDao;
import pl.register.android.database.tables.LessonDBTable;
import pl.register.android.homework.Homework;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.program.Program;
import pl.register.android.rating.Rating;
import pl.register.android.student.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */

public class StudentLessonCRUD implements StudentLessonDao {

    private SQLiteDatabase database;

    private ClassesCRUD classesCRUD;

    private HomeworkCRUD homeworkCRUD;

    public StudentLessonCRUD(SQLiteDatabase database) {
        this.database = database;
        classesCRUD = new ClassesCRUD(database);
        homeworkCRUD = new HomeworkCRUD(database);
    }

    // INSERT

    @Override
    public List<Lesson> findLessonsFor(Student student, Program program) {
        List<Lesson> lessons = new ArrayList<Lesson>();
        for (LessonProgram lessonProgram : program.getLessons()) {
            Lesson lesson = findLessonFor(student, lessonProgram);
            if (lesson != null) {
                lessons.add(lesson);
            }
        }
        return lessons;
    }

    @Override
    public Lesson findLessonFor(Student student, LessonProgram lessonProgram) {
        Lesson lesson = null;
        String where = LessonDBTable.FK_STUDENT_ID + " = " + student.getId() +
                " AND " + LessonDBTable.FK_LESSON_PROGRAM_ID + " = " + lessonProgram.getId();
        Cursor cursor = database.query(LessonDBTable.TABLE_NAME, null, where, null, null, null, null);
        while (cursor.moveToNext()) {
            Long lessonId = cursor.getLong(cursor.getColumnIndex(LessonDBTable.ID));
            Homework homework = homeworkCRUD.findHomeworkBy(lessonId, lessonProgram.getHomeworkTopic());
            List<Classes> classes = classesCRUD.findClassesBy(lessonId, lessonProgram.getClassesTopics());
            lesson = new Lesson(lessonId, classes, homework);
            lesson.setClassesDateString(lessonProgram.getClassesDateInString());
            lesson.setHomeworkDateString(lessonProgram.getHomeworkDateInString());
        }
        cursor.close();
        return lesson;
    }

    @Override
    public List<Lesson> insertLessonFor(Student student, Program program) {
        List<Lesson> studentLessons = new ArrayList<Lesson>();
        for (LessonProgram lessonProgram : program.getLessons()) {
            Lesson studentLesson = createFor(student, lessonProgram);
            studentLessons.add(studentLesson);
        }
        return studentLessons;
    }

    private Lesson createFor(Student student, LessonProgram lessonProgram) {
        Lesson studentLesson = new Lesson();
        ContentValues insertValues = new ContentValues();
        insertValues.put(LessonDBTable.FK_LESSON_PROGRAM_ID, lessonProgram.getId());
        insertValues.put(LessonDBTable.FK_STUDENT_ID, student.getId());
        long rowId = database.insert(LessonDBTable.TABLE_NAME, null, insertValues);
        studentLesson.setId(rowId);

        List<Classes> studentClasses = insertStudentClasses(studentLesson, lessonProgram);
        studentLesson.setClasses(studentClasses);
        Homework homework = insertStudentHomework(studentLesson, lessonProgram);
        studentLesson.setHomework(homework);
        return studentLesson;
    }

    private List<Classes> insertStudentClasses(Lesson lesson, LessonProgram lessonProgram) {
        List<Classes> studentClassesList = new ArrayList<Classes>();
        for (ClassesTopic classesTopic : lessonProgram.getClassesTopics()) {
            Classes studentClasses = new Classes(false, Rating.NONE);
            studentClasses = classesCRUD.insertStudentClasses(studentClasses, classesTopic, lesson);
            studentClassesList.add(studentClasses);
        }
        return studentClassesList;
    }

    private Homework insertStudentHomework(Lesson lesson, LessonProgram lessonProgram) {
        Homework studentHomework = new Homework(false, Rating.NONE);
        studentHomework = homeworkCRUD.insertHomework(studentHomework, lessonProgram.getHomeworkTopic(), lesson);
        return studentHomework;
    }

    //UPDATE
    public Homework updateStudentHomework(Homework homework) {
        return homeworkCRUD.updateHomework(homework);
    }

    public Classes updateStudentClasses(Classes classes) {
        return classesCRUD.updateClasses(classes);
    }

    //DELETE
    @Override
    public void deleteLessonsFrom(Student student, Program program) {
        List<Lesson> lessons = findLessonsFor(student, program);
        for (Lesson lesson : lessons) {
            classesCRUD.delete(lesson);
            homeworkCRUD.delete(lesson);
            String where = LessonDBTable.ID + " = " + lesson.getId();
            int res = database.delete(LessonDBTable.TABLE_NAME, where, null);
        }
    }
}